//
//  FloatpopulationContainerViewController.m
//  Utils
//
//  Created by enghou on 2018/4/26.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "FloatpopulationContainerViewController.h"
#import "FloatInfoViewController.h"
#import "NetWorkerManager.h"
@interface FloatpopulationContainerViewController ()

@end

@implementation FloatpopulationContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.slide configTitles:@[@"基本信息",@"流入信息",@"业务信息"]];
    FloatInfoViewController *f = (FloatInfoViewController *)[VCLoader vcWithName:@"FloatInfoViewController"];
    f.theDelete = self;
    self.ctlArray[1] = f;
    // Do any additional setup after loading the view.
}

-(void)deleAction{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/realizeFloatingPopulation/deleteFloatingPopulationById.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if ([self.theDelete respondsToSelector:@selector(beginRefresh)]) {
                [self.theDelete performSelector:@selector(beginRefresh)];
                [self pop];
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//-(void)configViewControllers{
//    FloatInfoViewController *f = (FloatInfoViewController *)[VCLoader vcWithName:@"FloatInfoViewController"];
//    f.theDelete = self;
//    self.ctlArray[1] = f;
//}

-(void)modifyWithFilePath:(NSString *)path{
    NSMutableDictionary *param = [self param];
    if ([path length] > 0) {
        [param setObject:path forKey:@"population.fileStorage.path"];
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/realizeFloatingPopulation/updateFloatingPopulation.action" parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if ([self.theDelete respondsToSelector:@selector(beginRefresh)]) {
                [self.theDelete performSelector:@selector(beginRefresh)];
            }
            [self pop];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)addNewWithFilePath:(NSString *)path{
    NSMutableDictionary *param = [self param];
    if ([path length] > 0) {
        [param setObject:path forKey:@"population.fileStorage.path"];
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/realizeFloatingPopulation/addFloatingPopulation.action" parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if ([self.theDelete respondsToSelector:@selector(beginRefresh)]) {
                [self.theDelete performSelector:@selector(beginRefresh)];
                [self pop];
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)loadData{
    if ([self.ids length] > 0) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/realizeFloatingPopulation/floatingPopulationDetail.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                NSDictionary *data = [model.data objectForKey:@"data"];
                PopulationBaseInfoViewController *p = [self.ctlArray objectAtIndex:0];
                NSLog(@"%@",p.view);
                if (data[@"imgUrl"]) {
                    p.head_imageurl = data[@"imgUrl"];
                    [p.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,p.head_imageurl]] placeholderImage:[UIImage imageNamed:@"icon_empty_head"]];
                }
                p.idCardField.text = data[@"idCardNo"];
                if ([data[@"idCardNo"] length] == 18) {
                    p.idCardField.enabled = NO;
                    p.calendarBtn.enabled = NO;
                }
                p.nameField.text = data[@"name"];
                p.sexField.text = data[@"gender"][@"displayName"];
                p.sexModel = [[PropertyDictItemModel alloc]init];
                p.sexModel.ids = [model contentForKey:@"id" withSource:data[@"gender"]];
                p.sexModel.displayName = [model contentForKey:@"displayName" withSource:data[@"gender"]];//data[@"gender"][@"displayName"];
                p.birthdayField.text = data[@"birthday"];
                p.locateTypeField.text = data[@"residenceType"][@"displayName"];
                p.registerTypeModel = [[PropertyDictItemModel alloc]init];
                p.registerTypeModel.displayName = data[@"residenceType"][@"displayName"];
                p.registerTypeModel.ids = [model contentForKey:@"id" withSource:data[@"residenceType"]];
                p.minzuField.text = data[@"nation"][@"displayName"];
                p.minzuModel = [[PropertyDictItemModel alloc]init];
                p.minzuModel.ids = [model contentForKey:@"id" withSource:data[@"nation"]];
                p.minzuModel.displayName = data[@"nation"][@"displayName"];
                p.hujiProvince = [[Province alloc]init];
                p.hujiProvince.code = [model contentForKey:@"province" withSource:data];//data[@"province"];
                p.hujiCity = [[City alloc]init];
                p.hujiCity.code = [model contentForKey:@"city" withSource:data];;
                p.hujiCountry = [[Country alloc]init];
                p.hujiCountry.code = [model contentForKey:@"district" withSource:data];;
                p.oldProvince = [[Province alloc]init];
                p.oldProvince.code = [model contentForKey:@"placeProvince" withSource:data];;
                p.oldCity = [[City alloc]init];
                p.oldCity.code = [model contentForKey:@"placeCity" withSource:data];
                p.oldCountry = [[Country alloc]init];
                p.oldCountry.code = [model contentForKey:@"placeDistrict" withSource:data];
                
                p.nowProvince = [[Province alloc]init];
                p.nowProvince.code = [model contentForKey:@"presentProvince" withSource:data];
                p.nowCity = [[City alloc]init];
                p.nowCity.code = [model contentForKey:@"presentProvince" withSource:data];
                p.nowCountry = [[Country alloc]init];
                p.nowCountry.code = [model contentForKey:@"presentProvince" withSource:data];
                
                p.politicalModel = [[PropertyDictItemModel alloc]init];
                p.politicalModel.ids = [model contentForKey:@"id" withSource:data[@"politicalBackground"]];
                
                p.marryModel = [[PropertyDictItemModel alloc]init];
                p.marryModel.ids = [model contentForKey:@"id" withSource:data[@"maritalState"]];
                
                p.eduDegreeModel = [[SubTypeItemModel alloc]init];
                p.eduDegreeModel.subTid = [model contentForKey:@"schoolVal" withSource:data];//data[@"schoolVal"];
                p.educationDegreeField.text = data[@"schoolStr"];
                
                p.occupyModel = [[PropertyDictItemModel alloc]init];
                p.occupyModel.ids = [model contentForKey:@"id" withSource:data[@"career"]];//data[@"career"][@"id"];
                
                p.religionModel = [[PropertyDictItemModel alloc]init];
                p.religionModel.ids = [model contentForKey:@"id" withSource:data[@"faith"]];
                
                p.zhengzhiField.text = data[@"politicalBackground"][@"displayName"];
                p.marriedField.text = data[@"maritalState"][@"displayName"];
                p.locateAddressField.text = data[@"domicilePlace"];
                p.homeLocateField.text = data[@"place"];
                p.homeLocateDetailField.text = data[@"nativePlaceAddress"];
                BOOL hasHouse = [data[@"isHaveHouse"] boolValue];
                p.nowLocateField.text = data[@"present"];
                if (hasHouse) {
                    p.yesBtn.enabled = YES;
                    p.yesBtn.selected = YES;
                    p.nowLocateDetailField.text = data[@"currentAddress"];
                    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*现住门(楼)详址" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:17]}];
                    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
                    p.nowLocateDetailField.placeholder = @"请输入门楼详址";
                    p.changeState.attributedText = target;
                }else{
                    p.noBtn.enabled = YES;
                    p.noBtn.selected = YES;
                    p.nowLocateDetailField.text = data[@"noHouseReason"];
                    NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*无住房原因" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]}];
                    [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:17]} range:NSMakeRange(0, 1)];
                    p.changeState.attributedText = target;
                    p.nowLocateDetailField.placeholder = @"请输无住房原因";
                }
                p.marriedField.text = data[@"maritalState"][@"displayName"];
                p.pastNameField.text = data[@"usedName"];
                p.phoneNumberField.text = [model contentForKey:@"mobileNumber" withSource:data];//data[@"mobileNumber"];
                p.stablePhoneField.text = [model contentForKey:@"telephone" withSource:data];//data[@"telephone"];
                p.occupyCategoryField.text = data[@"career"][@"displayName"];
                p.occupyField.text = data[@"careerText"];
                p.zongjiaoField.text = data[@"faith"][@"displayName"];
                p.serviceLocateField.text = data[@"workUnit"];
                FloatInfoViewController *po = self.ctlArray[1];
                NSLog(@"%@",po.view);
                po.locateModel.ids = [model contentForKey:@"id" withSource:data[@"stayLocationType"]];
                po.locateTypeField.text = [model contentForKey:@"displayName" withSource:data[@"stayLocationType"]];
                po.reasonModel.ids = [model contentForKey:@"id" withSource:data[@"inflowingReason"]];
                po.intoReasonField.text = [model contentForKey:@"displayName" withSource:data[@"inflowingReason"]];
                po.cardTypeModel.ids = [model contentForKey:@"id" withSource:data[@"registrationType"]];
                po.cardTypeField.text = [model contentForKey:@"displayName" withSource:data[@"registrationType"]];
                po.cardNumberField.text = data[@"certificateNumber"];
                po.registDateField.text = data[@"registerDate"];
                po.endDateField.text = data[@"expectedDatedue"];
                BOOL emphsize = [data[@"isEmphasis"] boolValue];
                po.yesBtn.selected = emphsize;
                po.noBtn.selected = !emphsize;
                QueryTypeSelectionController *query = self.ctlArray[2];
                
                NSLog(@"%@",query.view);
                NSMutableArray *selectedBusiness = [data[@"businessInformationPerson"] mutableCopy];
                NSArray *selectOrgs = data[@"railways"];
                if ([selectOrgs count] > 0) {
                    NSDictionary *firstOrg = selectOrgs[0];
                    [selectedBusiness enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj isEqualToString:@"alongRailwayPeople"]) {
                            [selectedBusiness removeObject:obj];
                        }
                    }];
                    query.selectedOrgId = firstOrg[@"orgId"];
                    query.orgType = firstOrg[@"type"];
                }
                query.selectedBusiness = [selectedBusiness mutableCopy];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
